﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using GlobalHelper;

namespace Standardization
{
    public class LocalHelper
    {
        /// <summary>
        /// 一汽判断路牌结束
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsEndCompanyOne(string s)
        {
            bool result = false;

            foreach (string flag in Helper.TokenLineEndFlag)
            {
                if (s.EndsWith(flag))
                {
                    result = true;
                    break;
                }
            }

            return result;
        }

        /// <summary>
        /// 五汽判断路牌结束标识
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsEndCompanyFive(string s)
        {
            bool result = false;

            foreach (string flag in Helper.TokenLineEndFlag)
            {
                if (s.Trim() == flag)
                {
                    result = true;
                    break;
                }
            }

            return result;
        }

        /// <summary>
        /// 判断是否标准的路牌结束
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsStandEnd(string s)
        {
            bool result = false;

            if (s.EndsWith(Helper.StandTokenEndFlag))
            {
                result = true;
            }

            return result;
        }

        /// <summary>
        /// 判断 字符串 为路牌名
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static bool IsTokenName(string content)
        {
            bool result = false;

            if (content.StartsWith(Helper.TokenBeginFlag)
                && content.EndsWith(Helper.TokenEndFlag))
            {
                result = true;
            }

            return result;
        }

        /// <summary>
        /// 判断 字符串 为路牌名起始
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsBeginWithTokenName(string s)
        {
            bool result = false;

            Regex regex = new Regex(@"^[<].*[>][0-9]*$");

            if (regex.IsMatch(s))
            {
                result = true;
            }

            return result;
        }

        /// <summary>
        /// 合并下半班路牌与发车时间
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static IList<string> CombinNightTokenName(IList<string> list)
        {
            IList<string> result = list;

            int position = -1;

            for (int i = 1; i < list.Count; i++)
            {
                if (LocalHelper.IsTokenName(list[i]))
                {
                    position = i;
                }
            }

            if (position != -1)
            {

                #region opertaor

                //1.4 路牌加标识
                if (!result[position].StartsWith(Helper.TokenBeginFlag))
                {
                    result[position] = Helper.TokenBeginFlag + result[position];
                }

                if (!result[position].EndsWith(Helper.TokenEndFlag))
                {
                    result[position] += Helper.TokenEndFlag;
                }

                //1.5 将路牌名合并入首个发车时间
                if (position == result.Count)
                {
                    throw new Exception("路牌名位于行末!");
                }
                if (Helper.CheckIsNumber(result[position + 1]))
                {
                    result[position + 1] = result[position] + result[position + 1];
                }

                //1.6 清除原路牌名单元
                result[position] = string.Empty;

                #endregion

            }

            return result;
        }
    }
}
